clear
clear mata
clear matrix
capture log close
program drop _all
macro drop _all
version 10.0
set mem 500m
set mat 2000
set more off
cd "E:\REStat_MS14767_Vol96(2)\Estimation"
log using "REStat_MS14797_Vol96(2)_estimation_compustat.log", replace


********************************************************************************
********                                                ************************
********      in this file we do the main regressions   ************************
********                                                ************************
********************************************************************************

use RESTATestimation_sample_final.dta, clear

* we drop one industry (59 observations + 1 obs) which has a wierd pattern
* we also drop industry 9997 ("industrial conglomerates") following the suggestion of one referee
* we finally drop industries with less than 4 firms

gen drop=0
replace drop=1 if SIC4==3241
replace drop=1 if SIC4==9997
replace drop=1 if ncomp<4
*replace drop=1 if network==1 
*drop if drop==1

xtset firmnum year


*************************************************************
**** preliminary statistics - Table 1
*************************************************************
gen RJVhorMS_s=0
replace RJVhorMS_s=1 if networkMS>0 & networkMS<.1668186

gen RJVhorMS_m=0
replace RJVhorMS_m=1 if networkMS>=.1668186 & networkMS<.5327271

gen RJVhorMS_l=0
replace RJVhorMS_l=1 if networkMS>=.5327271

tabstat MS sales_new ta rd2 rd_int patents network networkMS coverage RJVsametot links2_ver m_rdexpense  m_lmkval2 ncomp if RJV2==0 & zero_same==1, stat (mean min max sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents network networkMS coverage RJVsametot links2_ver m_rdexpense  m_lmkval2 ncomp if RJV2==1, stat (mean min max sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents network networkMS coverage RJVsametot links2_ver m_rdexpense  m_lmkval2 ncomp if RJVver==1, stat (mean min max sd n) c(st)
tabstat MS sales_new ta rd2 rd_int patents network networkMS coverage RJVsametot links2_ver m_rdexpense  m_lmkval2 ncomp if RJVhor==1, stat (mean min max sd n) c(st)


tabstat MS  sales_new ta rd2 rd_int patents network coverage RJVsametot links2_ver m_rdexpense  m_lmkval2 ncomp if RJVhor==1 & (network==0 | network<.031746), stat (mean min max sd n) c(st)
tabstat MS  sales_new ta rd2 rd_int patents network coverage RJVsametot links2_ver m_rdexpense  m_lmkval2 ncomp if RJVhor==1 & (network==0 | network>.031746 & network<.1948052),  stat (mean min max sd n) c(st)
tabstat MS  sales_new ta rd2 rd_int patents network coverage RJVsametot links2_ver m_rdexpense  m_lmkval2 ncomp if RJVhor==1 & (network==0 | network>.1948052), stat (mean min max sd n) c(st)

tabstat networkMS if RJVhor==1 & (networkMS==0 | networkMS<.1059457), stat (mean min max sd n) c(st)
tabstat networkMS if networkMS>=.1059457 & networkMS<.4082499, stat (mean min max sd n) c(st)
tabstat networkMS if networkMS>=.4082499, stat (mean min max sd n) c(st)


gen aa=1 if network>0 & network<.031746
replace aa=0 if network>.031746 & network<.1948052
ttest MS, by(aa)
gen bb=1 if aa==0
replace bb=0 if network>.1948052 & network!=.
ttest MS, by(bb)
drop aa bb


************************
***** UNIT ROOTS TESTS
************************

egen a=count(firmnum), by(firmnum)

*xtunitroot fisher MS if zero_same==0 & drop==0 & a>3, pperron lags(1)
*xtunitroot fisher MS if zero_same==0 & drop==0 & a>3, dfuller lags(1)


************************
***** Determinants of RJV participation - Table 3
************************

*we first define the estimation sample
xtdpd L(0/1).MS L(0/2).RJV2 L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJV, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(5/5).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(1/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep

xtprobit L(0/0).RJV2 L(3/3).MS patents_t_3 l(3/3).lta L(3/3).lrd2 L(1/1).m_lrd L(2/2).m_lmkval dy* if e(sample)
eststo RJV1p
xtprobit L(0/0).RJVver L(3/3).MS patents_t_3 l(3/3).lta L(3/3).lrd2 L(1/1).m_lrd L(2/2).m_lmkval dy*  if e(sample)
eststo RJV2p
xtprobit L(0/0).RJVhor L(3/3).MS  patents_t_3 l(3/3).lta L(3/3).lrd2 L(1/1).m_lrd L(2/2).m_lmkval dy* if e(sample)
eststo RJV3p
xtprobit L(0/0).RJVhor_s L(3/3).MS  L(3/3).MS patents_t_3 l(3/3).lta L(3/3).lrd2 L(1/1).m_lrd L(2/2).m_lmkval dy* if e(sample)
eststo RJV4p
xtprobit L(0/0).RJVhor_m L(3/3).MS  L(3/3).MS patents_t_3 l(3/3).lta L(3/3).lrd2 L(1/1).m_lrd L(2/2).m_lmkval dy* if e(sample)
eststo RJV5p
xtprobit L(0/0).RJVhor_l L(3/3).MS  L(3/3).MS patents_t_3 l(3/3).lta L(3/3).lrd2 L(1/1).m_lrd L(2/2).m_lmkval dy* if e(sample)
eststo RJV6p


************************
*********  RJV ANY - Table 4
************************


xtdpd L(0/1).MS L(0/2).RJV2 L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJV, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(5/5).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(1/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJV2 L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJV, lag(3 3)) dgmmiv(lrd2, lag(2 6))  div(l(1/1).patents_t_2 L(1/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJV2 L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 &  network>=0, dgmmiv(MS, lag(2 5)) dgmmiv(RJV, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(5/5).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(1/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo any
lincom RJV2+L1.RJV2+l2.RJV2
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(13,12.2)


************************
*********  HORIZONTAL vs. VERTICAL RJV - Table 4
************************

xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 , dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(3 5))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 , dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(3 5)) div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0 , dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(3 5))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo hor_ver
lincom RJVhor+L1.RJVhor+l2.RJVhor
lincom RJVver+L1.RJVver+l2.RJVver
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(12,10.2)

************************
*********  HORIZONTAL SMALL - MEDIUM - LARGE vs VERTICAL RJV - Table 4
************************


xtdpd L(0/1).MS L(0/2).RJVhor_s L(0/2).RJVhor_m L(0/2).RJVhor_l L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor_s, lag(3 3))  dgmmiv(RJVhor_m, lag(3 3))  dgmmiv(RJVhor_l, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJVhor_s L(0/2).RJVhor_m L(0/2).RJVhor_l L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor_s, lag(3 3))  dgmmiv(RJVhor_m, lag(3 3))  dgmmiv(RJVhor_l, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(2 6)) div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhor_s L(0/2).RJVhor_m L(0/2).RJVhor_l L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor_s, lag(3 3))  dgmmiv(RJVhor_m, lag(3 3))  dgmmiv(RJVhor_l, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo hor_sml
lincom RJVhor_s+L1.RJVhor_s+l2.RJVhor_s
lincom RJVhor_m+L1.RJVhor_m+l2.RJVhor_m
lincom RJVhor_l+L1.RJVhor_l+l2.RJVhor_l
lincom RJVver+L1.RJVver+l2.RJVver
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(13,13.1)

/*
predict MSpred
predict  MSpred_sd, stdp
gen lowerpred =MSpred- 1.96 * MSpred_sd
gen upperpred = MSpred+ 1.96 * MSpred_sd
*/

***********************
**** HORIZONTAL SMALL - MEDIUM - LARGE vs VERTICAL RJV  
****    network based on MS    -   Table 12
***********************


xtdpd L(0/1).MS L(0/2).RJVhorMS_s L(0/2).RJVhorMS_m L(0/2).RJVhorMS_l L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhorMS_s, lag(3 3))  dgmmiv(RJVhorMS_m, lag(3 3))  dgmmiv(RJVhorMS_l, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
*no system
xtdpd L(0/1).MS L(0/2).RJVhorMS_s L(0/2).RJVhorMS_m L(0/2).RJVhorMS_l L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhorMS_s, lag(3 3))  dgmmiv(RJVhorMS_m, lag(3 3))  dgmmiv(RJVhorMS_l, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(2 6)) div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhorMS_s L(0/2).RJVhorMS_m L(0/2).RJVhorMS_l L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhorMS_s, lag(3 3))  dgmmiv(RJVhorMS_m, lag(3 3))  dgmmiv(RJVhorMS_l, lag(3 3)) dgmmiv(RJVver, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo sml_ms
lincom RJVhorMS_s+L1.RJVhorMS_s+l2.RJVhorMS_s
lincom RJVhorMS_m+L1.RJVhorMS_m+l2.RJVhorMS_m
lincom RJVhorMS_l+L1.RJVhorMS_l+l2.RJVhorMS_l
lincom RJVver+L1.RJVver+l2.RJVver
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(13,11)

******
*** VERTICAL SMALL - MEDIUM - LARGE - Table 5
******

gen RJVver_s=0
replace  RJVver_s= RJVver if links2_ver<=7
gen RJVver_m=0
replace  RJVver_m= RJVver if links2_ver>7 & links2_ver<=43
gen RJVver_l=0
replace  RJVver_l= RJVver if links2_ver>43


xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_s L(0/2).RJVver_m L(0/2).RJVver_l L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor, lag(3 3)) dgmmiv(RJVver_s, lag(3 3))  dgmmiv(RJVver_m, lag(3 3))  dgmmiv(RJVver_l, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
*nosystem
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_s L(0/2).RJVver_m L(0/2).RJVver_l L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor, lag(3 3)) dgmmiv(RJVver_s, lag(3 3))  dgmmiv(RJVver_m, lag(3 3))  dgmmiv(RJVver_l, lag(3 3)) dgmmiv(lrd2, lag(2 6))  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).RJVhor L(0/2).RJVver_s L(0/2).RJVver_m L(0/2).RJVver_l L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(2 5)) dgmmiv(RJVhor, lag(3 3)) dgmmiv(RJVver_s, lag(3 3))  dgmmiv(RJVver_m, lag(3 3))  dgmmiv(RJVver_l, lag(3 3)) dgmmiv(lrd2, lag(2 6))  lgmmiv(l(6/6).MS l(6/6).lrd2)  div(l(1/1).patents_t_2 L(2/2).ta) div(L(1/2).m_lmkval L(1/2).m_lrd dy2-dy13) twostep vce(r)
estat abond
eststo ver_sml
lincom RJVhor+L1.RJVhor+l2.RJVhor
lincom RJVver_s+L1.RJVver_s+l2.RJVver_s
lincom RJVver_m+L1.RJVver_m+l2.RJVver_m
lincom RJVver_l+L1.RJVver_l+l2.RJVver_l
lincom lrd2+L1.lrd2+l2.lrd2

displ chi2(12,6.8)


**********************
** continuous effect - Figure 1
*******************

xtdpd L(0/1).MS L(0/2).network L(0/2).network2 L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(5 6)) dgmmiv(network, lag(5 6))  dgmmiv(network2, lag(5 6))  dgmmiv(RJVver, lag(3 4)) dgmmiv(lrd2, lag(3 4))  lgmmiv(l(4/5).MS l(4/5).lrd2)  div(l(1/2).patents_t_2 L(1/2).ta) div(L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13) twostep
estat sargan
xtdpd L(0/1).MS L(0/2).network L(0/2).network2 L(0/2).RJVver L(0/2).lrd2 L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13 if zero_same==0 & drop==0, dgmmiv(MS, lag(5 6)) dgmmiv(network, lag(5 6))  dgmmiv(network2, lag(5 6))  dgmmiv(RJVver, lag(3 4)) dgmmiv(lrd2, lag(3 4))  lgmmiv(l(4/5).MS l(4/5).lrd2)  div(l(1/2).patents_t_2 L(1/2).ta) div(L(1/1).m_lmkval L(1/1).m_lrd dy2-dy13) twostep vce(r)
estat abond
gen eff_net=0.001+(_b[network]+_b[L.network]+_b[L2.network])*network+(_b[network2]+_b[L.network2]+_b[L2.network2])*network2
label var eff_net "Heterogenous effect I"

twoway (line eff_net network,  xlabel(0 .1 .2 .3 .4 .5 .6 .7 ) ylabel(.01 0 -.01 -.02 -.03 -.04 -.05 -.06 ) xline(0.18) yline(-0.0137) yline(-0.0280) lwidth(thick) sort) if network<.7

*log using "graph_net.log", replace
forvalues x=0(.01)1 {
lincom (network + L1.network + L2.network)*`x'+(network2 + L1.network2 + L2.network2)*`x'*`x', l(95)
}
*log close


*************************
** correlation with the Gartner data
*************************


**************************
** since the Gartner data are proprietary we can not provide it.
** we therfore dropped them from our database after perfoming the command
** the command is then commented out
** the outcome of the command can be seen in the log file
**************************

*here we merge with the gartner data
/*
so ticker year
merge ticker year using "DRAM.dta

tab SIC4 if msh>0 & MS!=. & _merge==3
tab year if _merge==3
tab year if _merge==3 & msh>0
tab year if _merge==3 & msh>0 & MS>0
tab year if _merge==3 & msh>0 & MS>0 & MS!=.
pwcorr MS msh msh_ma if _merge==3 & msh>0 & MS!=., sig
pwcorr MS msh msh_ma if _merge==3 & msh>0 & MS!=. & SIC4==3674, sig
bysort year: pwcorr MS MSw msh msh_ma if _merge==3 & msh>0 & MS!=. & SIC4==3674, sig
*/

esttab RJV1p RJV2p RJV3p RJV4p RJV5p RJV6p using  "tables\REStat_MS14767_Vol96(2)_tables", replace label  nodepvar star(* 0.10 ** 0.05 *** 0.01) se  stats(N) drop(dy*) compress csv
esttab any hor_ver hor_sml using  "tables\REStat_MS14767_Vol96(2)_tables", append nodepvar star(* 0.10 ** 0.05 *** 0.01) se  stats(r N ) drop(dy*) label compress csv nogaps
esttab any ver_sml using  "tables\REStat_MS14767_Vol96(2)_tables", append nodepvar star(* 0.10 ** 0.05 *** 0.01) se  stats(r N ) drop(dy*) label compress csv nogaps
esttab any sml_ms using  "tables\REStat_MS14767_Vol96(2)_tables", append nodepvar star(* 0.10 ** 0.05 *** 0.01) se  stats(r N ) drop(dy*) label compress csv nogaps

log close


